(function (window) {
 
	// This library re-implements setTimeout, setInterval, clearTimeout, clearInterval for iOS6.
	// iOS6 suffers from a bug that kills timers that are created while a page is scrolling.
	// This library fixes that problem by recreating timers after scrolling finishes (with interval correction).
	// This code is free to use by anyone (MIT, blabla).
	// Author: rkorving@wizcorp.jp
 
	var timeouts = {};
	var intervals = {};
	var orgSetTimeout = window.setTimeout;
	var orgSetInterval = window.setInterval;
	var orgClearTimeout = window.clearTimeout;
	var orgClearInterval = window.clearInterval;


	function createTimer(set, map, args) {
		var id, cb = args[0], repeat = (set === orgSetInterval);

		function callback() {
				if (cb) {
						cb.apply(window, arguments);

						if (!repeat) {
								delete map[id];
								cb = null;
						}
				}
		}

		args[0] = callback;

		id = set.apply(window, args);

		map[id] = { args: args, created: Date.now(), cb: cb, id: id };

		return id;
	}


	function resetTimer(set, clear, map, virtualId, correctInterval) {
		var timer = map[virtualId];

		if (!timer) {
				return;
		}

		var repeat = (set === orgSetInterval);

		// cleanup

		clear(timer.id);

		// reduce the interval (arg 1 in the args array)

		if (!repeat) {
				var interval = timer.args[1];

				var reduction = Date.now() - timer.created;
				if (reduction < 0) {
						reduction = 0;
				}

				interval -= reduction;
				if (interval < 0) {
						interval = 0;
				}

				timer.args[1] = interval;
		}

		// recreate

		function callback() {
				if (timer.cb) {
						timer.cb.apply(window, arguments);
						if (!repeat) {
								delete map[virtualId];
								timer.cb = null;
						}
				}
		}

		timer.args[0] = callback;
		timer.created = Date.now();
		timer.id = set.apply(window, timer.args);
	}


	window.setTimeout = function () { return createTimer(orgSetTimeout, timeouts, arguments); };


	window.setInterval = function () { return createTimer(orgSetInterval, intervals, arguments); };

	window.clearTimeout = function (id) {
		var timer = timeouts[id];

		if (timer) {
				delete timeouts[id];
				orgClearTimeout(timer.id);
		}
	};

	window.clearInterval = function (id) {
		var timer = intervals[id];

		if (timer) {
				delete intervals[id];
				orgClearInterval(timer.id);
		}
	};

	window.addEventListener('scroll', function () {
		// recreate the timers using adjusted intervals
		// we cannot know how long the scroll-freeze lasted, so we cannot take that into account

		var virtualId;

		for (virtualId in timeouts) {
				resetTimer(orgSetTimeout, orgClearTimeout, timeouts, virtualId);
		}

		for (virtualId in intervals) {
				resetTimer(orgSetInterval, orgClearInterval, intervals, virtualId);
		}
	});
 
}(window));

/* Simple J-vaScript Inheritance

* By John Resig http://ejohn.org/

* MIT Licensed.

*/

// Inspired by base2 and Prototype

(function(){

  var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /./;
  // The base Class implementation (does nothing)

  this.Class = function(){};  // Create a new Class _this inherits from this class

  Class.extend = function(prop) {

    var _super = this.prototype;
    // Instantiate a base class (but only create the instance,

    // don't run the init constructor)

    initializing = true;

    var prototype = new this();

    initializing = false;
    // Copy the properties over onto the new prototype

    for (var name in prop) {

      // Check if we're overwriting an existing function

      prototype[name] = typeof prop[name] == "function" &&

        typeof _super[name] == "function" && fnTest.test(prop[name]) ?

        (function(name, fn){

          return function() {

            var tmp = this._super;
            // Add a new ._super() method _this is the same method

            // but on the super-class

            this._super = _super[name];
            // The method only need to be bound temporarily, so we

            // remove it when we're done executing

            var ret = fn.apply(this, arguments);

            this._super = tmp;
            return ret;

          };

        })(name, prop[name]) :

        prop[name];

    }
    // The dummy class constructor

    function Class() {

      // All construction is actually done in the init method

      if ( !initializing && this.init )

        this.init.apply(this, arguments);

    }
    // Populate our constructed prototype object

    Class.prototype = prototype;
    // Enforce the constructor to be what we expect

    Class.prototype.constructor = Class;
    // And make this class extendable

    Class.extend = arguments.callee;
    return Class;

  };

})();

$.config = {
	currentSlide: null,
	timer: null,
	myNn: []
};

;(function($) {
	var swipeCommon=Class.extend({
		isMobile:null,
		isAndroid:null,
		isIthing:null,
		elem:null,
		currentPosition:0,
		positionRange:100,
		autoRolling:false,
		rollingTime:0,
		animationTime:0,
		currentPaging:0,
		timer:null,
		paging:null,
		imgWidth:0,
		imgHeight:0,
		count:3,
		animationStatus:true,
		init: function(settings, options, elem) {
			this.options = jQuery.extend({}, settings, options);
			this.rollingTime=this.options.rollingTime;
			this.animationTime=this.options.animationTime;
			this.autoRolling=this.options.autoRolling;
			this.paging=this.options.paging;
			this.pagingActive=this.options.pagingActive;
			this.currentPaging=this.options.currentPaging;
			this.elem=elem;
			this.lookisMobile();
		},

		getSize:function(el,fn) {
			var img = new Image();
			img.onload=function() { fn(img.width,img.height); };
			img.src = el.attr('src');
		},

		lookisMobile: function () {
			this.isIthing = (/ipod|iphone|ipad/gi).test(navigator.appVersion);
			this.isAndroid = navigator.userAgent.match(/android/i) ? true : false;
			this.isWebkitAble();
			this.isMobile = (this.isIthing || this.isAndroid) ? true : false;
		},

		isWebkitAble: function () {
			return this.isWebkit = ('WebKitCSSMatrix' in window && 'm11' in new WebKitCSSMatrix()) ? true : false;
		},

		fix: function (event) {
			return event || window.event;
		},

		matrixToArray : function(matrix) {
			return matrix.substr(7, matrix.length - 8).split(', ');
		}
	});

	swipeUI=swipeCommon.extend({
		navigation:null,
		navigationWidth:null,
		navigationHeight:null,
		pagingHeight:null,
		bIsIphone:null,
		bIsHScroll:null,
		bIsVScroll:null,
		bIsSwiping:null,
		startX:null,
		startY:null,
		currentPaging:null,
		nSwipeThreshold:null, //at android 30, not android 25
		nCurrentWidth:null,
		currentSlide:null,
		sSwipeDir:null,
		aCurPos:[],
		init: function(settings, options, elem) {
			// Call the inherited version of swipeCommon
			var _this=this;

			this._super(settings, options, elem);

			this.bIsIphone=this.isIthing;
			this.navigation=jQuery(this.elem).find('li');

			this.pagingHeight=jQuery(this.elem).find(this.paging).innerHeight();
			this.createSwipe();
			//this.setEachPos(this.currentPaging);
			this.currentSlide=this.currentPaging;
			jQuery(this.paging).find('a').parent().removeClass('on').eq(this.currentSlide).addClass('on');
		},

		createSwipe:function() {
			var _this=this;

			// 紐⑤컮�쇱씠 �꾨땶 寃쎌슦
			this.setEachPos([0,jQuery(window).width(),jQuery(window).width()*2,jQuery(window).width()*-1]);
			this.bAutoRolling(); 
		},

		bAutoRolling:function(){
			var _this=this;
			
			if(this.autoRolling&&$(this.paging+' a').parent().index()>0) {
				this.timer=null;
				this.timer=setInterval(
					function() {
						_this.rolling(jQuery(window).width()*-1,jQuery(window).width(), $.config.currentSlide);
				},this.rollingTime);
				$.config.timer = this.timer;
			}
		},

		resizeEvent:function() {
			this.setEachPos(this.currentPosition);

		},

		setEachPos:function(currentPosition) {
			var _this=this,pos=this.currentPosition;
			this.navigationWidth = jQuery(window).width();
			this.nCurrentWidth = jQuery(window).width();//jQuery(window).width();

			this.onTouchReset();

			if (!this.isMobile) {
				this.navigation.each(
					function(idx) {
						_this.navigation.eq(idx).css({'left':pos+'px'});
					 }

				);
			} else {
				this.elem.find('li').each(
					function(idx) {
						$(this).css({
							'left':currentPosition[idx]+'px',
							'visibility':'visible'
						});
					}
				);
			}
			this.navigation.eq(this.currentPaging).css({'visibility':'visible'});

			this.bIsIphone=this.isIthing;
			//webkit browser�먯꽌�� img �ъ씠利� 援ы빐�ㅻ뒗 ��대컢�� � 釉뚮씪�곗졇� �곸씠�섏뿬 �대�吏 濡쒕뵫 �� �ъ씠利덈� 媛�몄삤�� �뺥깭濡� 援ы쁽
			/*
			if(this.elem.attr('id')=='mainSwipe'&&parseInt(this.elem.find('img').css('height'))>0){
				this.elem.css({'height':this.elem.find('img').css('height')});

			} else if(this.elem.attr('id')=='mainSwipe'&&parseInt(this.elem.find('img').css('height'))<1){
				this.elem.css({'height':jQuery(window).width()/2.9});
			}
			*/

		},

		setAnimation:function(currentSlide) {
			var _this=this;
			if(!this.isMobile) {
				this.navigation.each(function(idx) {
					if(jQuery(this).css('visibility')=='visible'){
						this.currentSlide=idx;
					}
				});
			}

			if(this.currentSlide==currentSlide) return;

			if(this.currentSlide < currentSlide) {
				//prev rolling
				this.rolling(_this.navigationWidth*-1,_this.navigationWidth,currentSlide);
			} else {
				//next rolling
				this.rolling(_this.navigationWidth,_this.navigationWidth*-1,currentSlide);
			}

		},
		rolling:function(prevLeft,nextLeft,currentSlide){
			var _this=this;
			if(this.animationStatus==true){
				this.animationStatus=false;
				this.navigation.eq(this.currentSlide).animate({'left':prevLeft+'px'},this.animationTime,function(){});

				if(typeof currentSlide == 'undefined') {
						this.currentSlide=currentSlide;
						$.config.currentSlide = this.currentSlide;
				} else {
					if (prevLeft<0)
					{
						if(this.navigation.size()-1==this.currentSlide) {
							this.currentSlide=0;
							$.config.currentSlide = this.currentSlide;
						} else {
							this.currentSlide = this.currentSlide +1;
							$.config.currentSlide = this.currentSlide;
						}
					}
					else {
						if(this.currentSlide<=0) {
							this.currentSlide= this.navigation.size()-1;
							$.config.currentSlide= this.currentSlide;
						} else {
							this.currentSlide = this.currentSlide -1;
							$.config.currentSlide = this.currentSlide;
						}
					}
				}
				// paging --
				this.currentPage(this.currentSlide);
				this.navigation.eq(this.currentSlide)
					.css({'left':nextLeft+'px', 'visibility':'visible'}).animate({'left':0},this.animationTime-10,function() {
						_this.animationStatus=true;
					});
			};
		},

		touchStartEvent:function(e) {
			//var oEvent = e.getOriginalEvent;
			var evt=this.fix(e);
			this.elem.find('li')
			// drag瑜� �쒖옉�섎㈃ �먮룞濡ㅻ쭅 醫낅즺
			clearInterval(this.timer);
			this.animationStatus = false;
			
			if(this.bIsIphone) {
				this.nSwipeThreshold=25;
			} else {
				this.nSwipeThreshold=30;
			}

			this.bIsHScroll = false;
			if(this.isAndroid) {
				if (window.timeOut) {
					window.clearTimeout(window.timeOut);
					window.timeOut = null;
				}

				this.nCurrentWidth = jQuery(window).width();//jQuery(window).width();
			}
			
			/* screen 媛믪쓣 �ъ슜�� �� �꾩씠�곗뿉�� 6.1�댄븯 踰꾩쟾�먯꽌 bounce�� �곗튂 �대룞 媛믪쓣 �섎せ怨꾩궛�섎뒗 �ㅻ쪟媛 諛쒖깮 
			this.startX = evt.touches[0].screenX;
			this.startY = evt.touches[0].screenY;
			*/
			this.startX = evt.targetTouches[0].pageX;
			this.startY = evt.targetTouches[0].pageY;

		},

		touchMoveEvent:function(e) {
			//touch event �몄텧 �� �먮컮�ㅽ겕由쏀듃 �대깽�� 媛앹껜 �몄텧 �뺥깭�닿린 �뚮Ц�� this媛 namespace瑜� 媛瑜댄궎�붽쾬�� �꾨땶 �щ씪�대뜑 �먯껜瑜� 媛瑜댄궎寃� �쒕떎.
			//var oEvent = e.getOriginalEvent;
			var evt=this.fix(e);
			var nMoveWidth=0, nMoveX=evt.targetTouches[0].pageX - this.startX, nMoveY= evt.targetTouches[0].pageY - this.startY,
			threshold = Math.abs(nMoveX) - Math.abs(nMoveY);
				

			if(!this.bIsHScroll&&threshold>-1) {
				evt.stopPropagation();
		        evt.preventDefault();				
				this.onTouchMove(nMoveX);
				this.bIsSwiping=true;
			} else {
				this.bIsHScroll=true;
			}
		},

		touchEndEvent:function(e) {
			var evt=this.fix(e);
			
			var nMoveX=evt.changedTouches[0].pageX-this.startX,
				nMoveY= evt.changedTouches[0].pageY-this.startY;

			if(!this.bIsHScroll && Math.abs(Math.round((nMoveX/this.nCurrentWidth)*200)) > this.nSwipeThreshold) {

				if(this.sSwipeDir=='prev') {
					this.movePrev();
					
					if(this.currentSlide<=0) {
						this.currentSlide= this.navigation.size()-1;
						$.config.currentSlide= this.currentSlide;
					} else {
						this.currentSlide = this.currentSlide -1;
						$.config.currentSlide = this.currentSlide;
					}						
				} else {
					this.moveNext();
					
					if(this.navigation.size()-1==this.currentSlide) {
						this.currentSlide=0;
						$.config.currentSlide = this.currentSlide;
					} else {
						this.currentSlide = this.currentSlide +1;
						$.config.currentSlide = this.currentSlide;
					}
				}
			} else {
				this.onTouchReset();
			}
			
			this.currentPage($.config.currentSlide);
			
			// drag媛 �꾨즺�섎㈃ �먮룞濡ㅻ쭅 �쒖옉
			this.animationStatus = true;
			this.bIsSwiping=false;
			this.bAutoRolling();
		},

		onTouchReset:function() {
			var currSlide = this.currentSlide;
			var prevSlide = this.currentSlide-1;
			var nextSlide = this.currentSlide+1;
			
			this.elem.find('li').each(function(idx){
				if(idx == currSlide) {
					$(this).css({'left':'0px'});
				}else if(idx == prevSlide) {
					$(this).css({'left':jQuery(window).width()*-1+'px'});
				}else {
					$(this).css({'left':jQuery(window).width()+'px'});
				}
			});
		},
		
		goSlide:function(currentSlide) {
			// drag瑜� �쒖옉�섎㈃ �먮룞濡ㅻ쭅 醫낅즺
			clearInterval(this.timer);
			this.animationStatus = false;
			var indecate = "next"; //濡ㅻ쭅 吏꾪뻾諛⑺뼢 湲곕낯媛�
			var animate = true;
			
			if(typeof currentSlide != "undefined"){
				if(currentSlide == 'prev') {
					indecate = "prev";
					
					if(this.currentSlide<=0) {
						this.currentSlide= this.navigation.size()-1;
						$.config.currentSlide= this.currentSlide;
					} else {
						this.currentSlide = this.currentSlide -1;
						$.config.currentSlide = this.currentSlide;
					}
				}else if(currentSlide == 'next') {
					indecate = "next";
					
					if(this.navigation.size()-1==this.currentSlide) {
						this.currentSlide=0;
						$.config.currentSlide = this.currentSlide;
					} else {
						this.currentSlide = this.currentSlide +1;
						$.config.currentSlide = this.currentSlide;
					}					
				}else {
					if(this.currentSlide < currentSlide) {
						indecate = "next";
					}else if(this.currentSlide > currentSlide) {
						indecate = "prev";
					}else {
						animate = false; // �숈씪�� �щ씪�대뱶硫� �대깽�� �놁쓬					
					}
					
					this.currentSlide = currentSlide;
					$.config.currentSlide = this.currentSlide;
				}
			}
			
			if(animate) {
				var currSlide = this.currentSlide;
				var prevSlide = currSlide-1;
				var nextSlide = currSlide+1;
				
				if(indecate == "prev") {
					this.elem.find('li').each(function(idx){
						if(idx == currSlide) {
							$(this).css({'left':jQuery(window).width()*-1+'px','visibility':'visible'}).stop(true,true).animate({'left':'0px'},200);
						}else if(idx == nextSlide) {
							$(this).css({'left':'0px'}).stop(true,true).animate({'left':jQuery(window).width()+'px'},200);
						}else {
							//$(this).stop(true,true).css({'left':0+'px'});
							// 2012-11-23 �댁닔吏� �섏젙_�� 泥섎읆 �섎㈃ 留덉�留� �대�吏�먯꽌 �꾨떒怨꾨� �쒖쇅�� �ㅻⅨ �대�吏 �대┃�� 留덉�留� �대�吏媛 �몄텧��
							$(this).css({'left':jQuery(window).width()+'px'});
						}
					});
				}else {
					this.elem.find('li').each(function(idx){
						if(idx == currSlide) {
							$(this).stop(true,true).css({'left':jQuery(window).width()+'px','visibility':'visible'}).animate({'left':'0px'},200);
						}else if(idx == prevSlide) {
							$(this).stop(true,true).css({'left':'0px'}).animate({'left':jQuery(window).width()*-1+'px'},200);
						}else {
							$(this).stop(true,true).css({'left':jQuery(window).width()+'px'});
						}
					});					
				}

				this.currentPage(currSlide);
			}
			
			// drag媛 �꾨즺�섎㈃ �먮룞濡ㅻ쭅 �쒖옉
			this.animationStatus = true;
			this.bAutoRolling();
		},

		onTouchMove:function(per) {
			var _this=this;
			per = parseInt(per); // %�멸꼍�곗뿉 �ъ슜

			var prevSlide = this.currentSlide-1;
			var nextSlide = this.currentSlide+1;
			var slideSize = this.navigation.size()-1;
			
			if(per > 0) {
				if(prevSlide<0) {
					prevSlide=slideSize;
				}
				
				this.sSwipeDir='prev';
			} else {
				if(nextSlide>slideSize) {
					nextSlide=0;
				}
				
				this.sSwipeDir='next';
			}

			this.elem.find('li').eq(prevSlide).css({'left':(per - jQuery(window).width())+'px'});
			this.elem.find('li').eq(this.currentSlide).css({'left':per+'px'});
			this.elem.find('li').eq(nextSlide).css({'left':(jQuery(window).width() + per)+'px'});
			
			if(this.elem.find('li').size() < 3&&this.sSwipeDir=='prev') {
				this.elem.find('li').eq(nextSlide).css({'left':(per - jQuery(window).width())+'px'});
			}
		},

		movePrev:function() {
			var currSlide = this.currentSlide;
			var prevSlide = this.currentSlide-1;
			var nextSlide = this.currentSlide+1;
			var slideSize = this.navigation.size()-1;
			
			if(prevSlide<0) {
				prevSlide=slideSize;
			}

			this.elem.find('li').each(function(idx){
				if(idx == currSlide) {
					$(this).stop(true,true).animate({'left':jQuery(window).width()+'px'},100);
				}else if(idx == prevSlide) {
					$(this).stop(true,true).animate({'left':'0px'},100);
				}else {
					$(this).css({'left':jQuery(window).width()+'px'});
				}
			});
		},

		moveNext:function() {
			var currSlide = this.currentSlide;
			var prevSlide = this.currentSlide-1;
			var nextSlide = this.currentSlide+1;
			var slideSize = this.navigation.size()-1;			

			if(nextSlide>slideSize) {
				nextSlide=0;
			}
			
			this.elem.find('li').each(function(idx){
				if(idx == currSlide) {
					$(this).stop(true,true).animate({'left':jQuery(window).width()*-1+'px'},100);
				}else if(idx == nextSlide) {
					$(this).stop(true,true).animate({'left':'0px'},100);
				}else {
					$(this).css({'left':jQuery(window).width()+'px'});
				}
			});
		},

		currentPage:function(currentSlide){
				jQuery(this.paging).find('a').parent().removeClass('on').eq(currentSlide).addClass('on');

			}
	   });
	swipeEvent=swipeUI.extend({

		init: function(settings, options, elem) {

			// Call the inherited version of swipeCommon and swipeUI;
			this._super(settings, options, elem);
			this.options = jQuery.extend({}, settings, options);
			var _this=this;

			// addEvent orientation or resize
			(typeof window.orientationchange != 'undefined') ? jQuery(window).bind('orientation', function() {
				_this.resizeEvent();
			})
			: jQuery(window).bind('resize', function() {
				_this.resizeEvent();
			});
			// web Event --

			if(!this.isMobile) {

				jQuery(this.options.paging+' a').bind('click',function() {
					_this.setAnimation(jQuery(this).parent().index());
				})

				elem
				.bind('mouseover',function() {
					clearInterval(_this.timer);
					_this.autoRolling=false;
				})

				.bind('mouseout',function() {
					if(_this.options.autoRolling) {
						_this.autoRolling=true;
						_this.bAutoRolling();
					}

				});

				$('.button-prev').css({'position':'absolute','bottom':'0','right':'100'}).click(function() {
					//prev rolling
					_this.rolling(_this.navigationWidth,_this.navigationWidth*-1,this.currentSlide);
				});

				$('.button-next').css({'position':'absolute','bottom':'0','right':'0'}).click(function() {
					//next rolling
					_this.rolling(_this.navigationWidth*-1,_this.navigationWidth,this.currentSlide);
				});


			} else {
				// mobile Event --

				document.getElementById(elem.attr('id')).addEventListener('touchstart',jQuery.proxy(this.touchStartEvent,this),false);
				document.getElementById(elem.attr('id')).addEventListener('touchmove',jQuery.proxy(this.touchMoveEvent,this),false);
				document.getElementById(elem.attr('id')).addEventListener('touchend',jQuery.proxy(this.touchEndEvent,this),false);
			}
			
		}

	});
	// Start a plugin

	jQuery.fn.swipe = function(options) {
		// Don't act on absent elements -via Paul Irish's advice
		if ( this.length ) {

			return this.each(function(idx){
				// Create a new nn object via the Prototypal Object create
				var settings = $.extend({
					paging:'div.paging-navi',
					currentPaging:0,
					pagingActive:'on',
					rollingTime:2000,
					animationTime:500,
					autoRolling:true
				},options);
				var myNn = new swipeEvent(settings,options,jQuery(this));
				$.config.myNn.push(myNn);
			});
		}
	};
})(jQuery);